/**
 * @author Ivan.Ma
 */
package com.sf.algo.solve.solver;

import org.optaplanner.core.api.score.buildin.hardsoftlong.HardSoftLongScore;
import org.optaplanner.core.impl.score.director.easy.EasyScoreCalculator;

import com.sf.algo.solve.domain.Goods;
import com.sf.algo.solve.domain.Location;
import com.sf.algo.solve.domain.Person;
import com.sf.algo.solve.domain.Solution;

/**
 * @author Ivan.Ma
 */
public class ScoreCalc implements EasyScoreCalculator<Solution> {

	@Override
	public HardSoftLongScore calculateScore(Solution solution) {
		long hardScore = 0;
		long softScore = 0;
		for (Person person : solution.getPersonList()) {
			if (person.getNext() != null) {
				Goods current = person.getNext();
				int step = 0;
				Location previous = person.getLocation();
				while (current != null) {
					int currentStep = current.getLocation().getDistance(previous);
					int tempStep = step + currentStep;
					int value = current.getValue() - tempStep;
					if (value > 0) {
						if ((step + currentStep) <= solution.getStepLimit()) {
							softScore += value;
							step = tempStep;
							previous = current.getLocation();
						} else {
							break;
						}
					}
					current = current.getNext();
				}
			}
		}
		return HardSoftLongScore.valueOf(hardScore, softScore);
	}


}
